//****************************************************************************
// Copyright (c) 2011-2013,WebJet Business Division,CYOU

// http://www.genesis-3d.com.cn

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//*****************************************************************************
Shader "PostEffect_SunShaft1" {
	Parameters{	
		Vector [texToTexParams] = "0.00066713482, 0.00036231885, -0.00017556180, 0.0013913043"

		Vector [SunShafts_SunPos] = "30.000000,60.387765,0,1"
		Vector [SunShafts_SunCol]   = "0.8,0.8,0.6,1"

		Vector [PI_sunShaftsParam1] = "0.1,0.76319116,0.0,0.0"
		Vector [PI_sunShaftsParam4] = "0.0050584702,1.1844447,0.0,0.0"
		
		sampler2D [p4RT] = "white"
	}
	
	RenderQueue "CUSTOMDRAW"
	

			CODEBEGIN

			float4		SunShafts_SunPos;
			float4      PI_sunShaftsParam1;
			
			
			struct VS_OUTPUT
			{
				float4 Position		: POSITION0;   // vertex position 
				float4 PosTex		: TEXCOORD0;//xy is positon, and zw is texCoords coord
			};
			struct PS_INPUT
			{
				float4 PosTex		: TEXCOORD0;
			};
			
			VS_OUTPUT mainVs(in float3 vPos : POSITION)
			{
				VS_OUTPUT Output;
								
				Output.Position.xy = vPos.xy;	
				Output.Position.zw = float2(0.9f, 1.0f);
				Output.PosTex.xy = vPos.xy;//pos
				Output.PosTex.zw = 0;
				return Output;
			}

			float4 mainPs(PS_INPUT o) : COLOR0
			{
				float2 uv = getUVfromscreenspace(o.PosTex.xy);
			    SunShafts_SunPos = mul(g_View,SunShafts_SunPos);
				SunShafts_SunPos = mul(g_Proj,SunShafts_SunPos);
				SunShafts_SunPos = SunShafts_SunPos.xyzw/SunShafts_SunPos.w;
				
				float2 sunPosProj = getUVfromscreenspace(SunShafts_SunPos.xy); //((IN.sunPos.xy / IN.sunPos.z)); 				
							  
				float2 sunVec = ( sunPosProj.xy - uv );
				  
				float fAspectRatio =  g_ScreenSize.y /g_ScreenSize.x;
				  
	
				float len = length(sunVec )*PI_sunShaftsParam1.y;
				len = 1 - clamp (len, 0.0, 1.0);
				float sunDist = clamp(len, 0.0, 1.0);
				float2 sunDir =  ( sunPosProj.xy - uv);  
				  
				float4 accum = 0; 
				sunDir.xy *= PI_sunShaftsParam1.x;
					
				  
				float2 jitter = 0;

				float4 depth = tex2D(g_MainBuffer, uv );      
				accum += depth;


				depth = tex2D(g_MainBuffer, uv + sunDir.xy * (1.0+jitter) );      
				accum += depth * (1.0-1.0/8.0);

				depth = tex2D(g_MainBuffer, (uv + sunDir.xy * (2.0+jitter)) );      
				accum += depth * (1.0-2.0/8.0);
				  
				depth = tex2D(g_MainBuffer, (uv + sunDir.xy * (3.0+jitter)) );      
				accum += depth * (1.0-3.0/8.0);
				  
				depth = tex2D(g_MainBuffer, (uv + sunDir.xy * (4.0+jitter)) );      
				accum += depth * (1.0-4.0/8.0);
				  
				depth = tex2D(g_MainBuffer, (uv + sunDir.xy * (5.0+jitter)) );      
				accum += depth * (1.0-5.0/8.0);
				  
				depth = tex2D(g_MainBuffer, (uv + sunDir.xy * (6.0+jitter)) );      
				accum += depth * (1.0-6.0/8.0);

				depth = tex2D(g_MainBuffer, (uv + sunDir.xy * (7.0+jitter)) );      
				accum += depth * (1.0-7.0/8.0);

				accum /= 8.0;

				float4 gl_FragColor = accum * 2  * float4(sunDist.xxx, 1);
				gl_FragColor.w += 1.0 -  clamp( (0.1 + 0.9),0.0, 1.0 );	  
			
				return gl_FragColor;
			}
			CODEEND
	
	Technique "PostEffect_SunShaft1"{
		Pass {
		    Compiler{
		        VertexShader = mainVs
				PixelShader = mainPs
		    }
		
			RenderSTATE { 
							cullMode "Back"
							ZTest Off
							ZWrite Off
							blendMode Off
							alphatest Off
							samplerState "g_MainBuffer" [Clamp] [Point]
							samplerState "g_DepthMap" [Clamp] [Point]
				    }
			}
		}

}



                                                                                                               